perm filename A108.TEX[106,PHY] blob sn#807809 filedate 1985-11-15 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00002 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	\magnification\magstephalf
C00007 ENDMK
C⊗;
\magnification\magstephalf
\input macro.tex
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
\baselineskip 14pt
\font\rmn=amr9
\rm
\line{\sevenrm a108.tex[106,phy] \today\hfill}
\bigskip
\line{\bf Programming Cliches\hfil}

In ordinary language, there are phrases or sentences we use over and over
again, like ``Do you swear to tell the truth, the whole truth and nothing
but the truth?'', or ``Ham on rye, hold the mustard.''  We know that they
serve their function precisely, and we save effort by not having to
reinvent them constantly.  Such cliches are found in computer programs too.
This book presents a number of them; they are worth memorizing.  Here is
one to exchange the contents of two variables.

To exchange the places of two cars in parking spaces, we must have a third
place to put a car.  If car~{\tt A} is in space~{\tt X} and 
car~{\tt B} is in space~{\tt Y}, we
move {\tt A}~from space~{\tt X} to a third space~{\tt T}, 
{\tt B}~from space~{\tt Y} to space~{\tt X}, and 
{\tt A}~from space~{\tt T} to space~{\tt Y}.  
In programs, we don't give numbers names that
follow them around, like ``car~{\tt A}'' above; we name them by saying where they
are.  The program fragment, analogous to the car swap, that exchanges the
numbers in variables {\tt X} and~{\tt Y} is

{\obeylines\obeyspaces\let =\ \tt
        T:=X;
        X:=Y;
        Y:=T
}

\noindent
where {\tt X}, {\tt Y}, and {\tt T} 
are variables of the same type, and {\tt T} is what we call a
temporary variable; that is, the value put into it is used at once and
never again.

Another cliche is used to find the value of $i$ for which $f(i)$
is a maximum ($A≤i≤B$); 

{\obeylines\obeyspaces\let =\ \tt
        MAX:=F(A); (* SMALLEST POSSIBLE VALUE *)
        LOCMAX:=A;
        FOR I:=A+1 TO B DO
            IF F(I)>MAX THEN
                BEGIN
                MAX:=F(I);
                LOCMAX:=I
                END;
}

\noindent
This cliche embodies the insight that finding the largest of a finite set
does not require that every element be compared to every other.

\medskip
\line{\bf Priority Cliches\hfil}

If a program must choose between several courses of action, often there is
a priority order; if the conditions for several actions are all true, the
program must perform the action of highest priority.

\smallskip
\line{\bf Example:\hfil}

{\obeylines\obeyspaces\let =\ \tt
        IF heart attack THEN administer CPR
        ELSE IF house on fire THEN call 911
        ELSE IF hungry THEN go for food
        ELSE IF unprepared THEN study.
}


\bigskip
\line{\copyright 1984 Robert W. Floyd; 
First draft (not published) March 28, 1984\hfil}
%revised: Date; subsequently revised.\hfill}

\bye